package com.zhongfeng.api.modules.customer.controller;

import com.zhongfeng.api.modules.customer.service.CustomerService;
import com.zhongfeng.api.modules.customer.vo.PrefixVo;
import com.zhongfeng.api.modules.customer.vo.params.CustomerParams;
import com.zhongfeng.api.modules.end.vo.params.EndParams;
import com.zhongfeng.api.modules.utils.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @author blueCat
 * @date 2023/2/8 10:17
 * @motto life is a gift
 */
@RestController
@RequestMapping("/api/customer")
@Slf4j
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 查询全部客户信息
     *
     * @param params
     * @return
     */
    @PostMapping("/queryAllCustomer")
    public Result queryAllCustomer(@RequestBody CustomerParams params) {
        return customerService.queryAllCustomer(params);
    }

    /**
     * 新增客户信息
     *
     * @param params
     * @return
     */
    @PostMapping("/addCustomer")
    public Result addCustomer(@RequestBody CustomerParams params) {
        return customerService.addCustomer(params);
    }

    /**
     * 删除客户信息
     *
     * @param params
     * @return
     */
    @PostMapping("/deleteCustomer")
    public Result deleteCustomer(@RequestBody CustomerParams params) {
        return customerService.deleteCustomer(params.getId());
    }

    /**
     * 修改客户信息
     *
     * @param params
     * @return
     */
    @PostMapping("/updateCustomer")
    public Result updateCustomer(@RequestBody CustomerParams params) {
        return customerService.updateCustomer(params);
    }

    /**
     * 文件加白
     *
     * @param file
     * @param request
     * @param response
     */
    @PostMapping("/addWhite")
    public String addWhite(@RequestParam MultipartFile file, @RequestParam String length, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //文件地址
        int num = Integer.parseInt(length);
        //存储结果集
        List<String> result = new ArrayList<>();
        Scanner sc = new Scanner(file.getInputStream());
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if (line.length() == 11) {
                line = line.substring(0, num);
                if (!result.contains(line)) {
                    result.add(line);
                }
            }
        }
        String resultNumber = "";
        for (String s : result) {
            resultNumber = resultNumber + "," + s;
        }
        return resultNumber;
    }


    /**
     * 整理号码
     * @param vo
     * @return
     */
    @PostMapping("/updateWhite")
    public String updateWhite(@RequestBody PrefixVo vo) throws IOException {
        //保留几位(默认为4)
        int num = Integer.parseInt(vo.getLength());
        //存储结果集
        List<String> result = new ArrayList<>();
        ByteArrayInputStream numberStream = new ByteArrayInputStream(vo.getUnWhiteNumber().getBytes(StandardCharsets.UTF_8));
        Scanner sc = new Scanner(numberStream);
        String next = sc.next();
        String[] split = next.split(",");
        List<String> numbers = Arrays.asList(split);
        String substring = "";
        for (String number : numbers) {
            if (number.length() > num) {
                substring = number.substring(0, num);
            } else {
                substring = number;
            }
            if (!result.contains(substring)) {
                result.add(substring);
            }
        }
        String resultNumber = "";
        int count = 0;
        for (String s : result) {
            if (count == 0) {
                resultNumber += s;
                count++;
            } else {
                resultNumber = resultNumber + "," + s;
            }
        }
        return resultNumber;
    }

    /**
     * 字符串加白
     * @param vo
     * @return
     */
    @PostMapping("/addWhite2")
    public String addWhite2(@RequestBody PrefixVo vo) throws IOException {
        //保留几位(默认为4)
        int num = Integer.parseInt(vo.getLength());
        //存储结果集
        List<String> result = new ArrayList<>();
        ByteArrayInputStream numberStream = new ByteArrayInputStream(vo.getUnWhiteNumber().getBytes(StandardCharsets.UTF_8));
        Scanner sc = new Scanner(numberStream);
        String resultNumber = "";
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            String[] split = line.split(",");
            List<String> numberList = new ArrayList<>();
            for (String s : split) {
                numberList.add(s);
            }
            Collections.shuffle(numberList);
            for (String s : numberList) {
                resultNumber = resultNumber + "\n" + s;
            }
        }
        return resultNumber;
    }
}
